import {
  HTTP
} from '../../utils/http'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workList: [], // 查看其他用户时数据
    workPageNum: 1,
    workPageSize: 10,
    workTotal: 0,
    publicWorkList: [], // 公开数据
    publicWorkPageNum: 1,
    publicWorkPageSize: 10,
    publicWorkTotal: 0,
    privateWorkList: [], // 私密数据
    privateWorkPageNum: 1,
    privateWorkPageSize: 10,
    privateWorkTotal: 0,
    userInfo: {
      id: null
    },
    isMe: false, // 是否查看自己
    tabActiveTitle: "公开",
    optionsAccountId:null
  },

  getUserListData(userId, param, success) {
    let data = Object.assign({
      "accountId": userId,
      "type": 0
    }, param)
    http.request({
      url: '/ai/draw/picture/get/account/page',
      method: 'post',
      data: data,
      success: (res) => {
        if (res.code === "0") {
          success && success(res)
        }
      }
    })
  },

  /**
   * 获取用户数据逻辑
   * 1.当userInfo登录的用户id与url参数传递的id一致时，表示为查看自己的数据，可以看公开和私密
   * 2.当userInfo登录的用户id与url参数传递的id不一致是，表示为查看其他用户的数据，仅查看公开数据
   */
  getListData(urlId) {
    if (urlId == this.data.userInfo.id) {
        this.getPublicList()
        this.getPrivateList()
    } else {
      this.getWorkList(urlId)
    }
  },

  getWorkList(urlId) {
    this.getUserListData(urlId, {
      "isPrivate": 0,
      "pageNum": this.data.workPageNum,
      "pageSize": this.data.workPageSize
    }, (res) => {
      let list = res.data.list
      var arr1 = this.data.publicWorkList;
      var arr2 = list;
      arr1 = arr1.concat(arr2);
      this.setData({
        workList: arr1,
        workTotal: res.data.total
      })
    })
  },

  getPrivateList() {
    // 获取私密数据
    this.getUserListData(this.data.userInfo.id, {
      isPrivate: 1,
      "pageNum": this.data.privateWorkPageNum,
      "pageSize": this.data.privateWorkPageSize
    }, (res) => {
      let list = res.data.list
      var arr1 = this.data.privateWorkList;
      var arr2 = list;
      arr1 = arr1.concat(arr2);
      this.setData({
        privateWorkList: arr1,
        privateWorkTotal: res.data.total
      })
    })
  },

  getPublicList() {
    // 获取公开数据
    this.getUserListData(this.data.userInfo.id, {
      isPrivate: 0,
      "pageNum": this.data.publicWorkPageNum,
      "pageSize": this.data.publicWorkPageSize
    }, (res) => {
      let list = res.data.list
      var arr1 = this.data.publicWorkList;
      var arr2 = list;
      arr1 = arr1.concat(arr2);
      this.setData({
        publicWorkList: arr1,
        publicWorkTotal: res.data.total
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userId = wx.getStorageSync('userId')
    this.setData({
      "userInfo.id": userId,
      isMe: options.accountId == userId,
      optionsAccountId:options.accountId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getListData(this.data.optionsAccountId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      workList: [], // 查看其他用户时数据
      workPageNum: 1,
      workPageSize: 10,
      publicWorkList: [], // 公开数据
      publicWorkPageNum: 1,
      publicWorkPageSize: 10,
      privateWorkList: [], // 私密数据
      privateWorkPageNum: 1,
      privateWorkPageSize: 10
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  onChange(e) {
    this.setData({
      tabActiveTitle: e.detail.title
    })
    if (e.detail.title === '公开') {
      this.setData({
        publicWorkList: [],
        publicWorkPageNum: 1,
        publicWorkPageSize: 10,
        publicWorkTotal: 0
      })
      this.getPublicList()
    } else {
      this.setData({
        privateWorkList: [],
        privateWorkPageNum: 1,
        privateWorkPageSize: 10,
        privateWorkTotal: 0
      })
      this.getPrivateList()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isMe) {
      if (this.data.tabActiveTitle === "公开") {
        if (!(this.data.publicWorkList && this.data.publicWorkList.length > 0)) return
        let currentTotal = this.data.publicWorkList.length;
        if (currentTotal <= this.data.publicWorkTotal) {
          var current = this.data.publicWorkPageNum * 1 + 1;
          this.setData({
            publicWorkPageNum: current
          })
          this.getPublicList();
        }
      } else if (this.data.tabActiveTitle === "私密") {
        if (!(this.data.privateWorkList && this.data.privateWorkList.length > 0)) return
        let currentTotal = this.data.privateWorkList.length;
        if (currentTotal <= this.data.privateWorkTotal) {
          var current = this.data.privateWorkPageNum * 1 + 1;
          this.setData({
            privateWorkPageNum: current
          })
          this.getPrivateList();
        }
      } else {
        if (!(this.data.workList && this.data.workList.length > 0)) return
        let currentTotal = this.data.workList.length;
        if (currentTotal <= this.data.workTotal) {
          var current = this.data.workPageNum * 1 + 1;
          this.setData({
            workPageNum: current
          })
          this.getWorkList();
        }
      }

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})